package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-03-02
 * Time: 00:31
 */
//class Test2{}

class Func{
    public long count = 0L;
    synchronized public void add(){
        count++;
    }

//    public void add(){
//        synchronized (Func.class) {
//            count++;
//        }
//    }
}

public class Thread_Safe_Two {

    public static void main(String[] args) throws InterruptedException {
        //Test2 test = new Test2();
        Func func = new Func();
        Thread thread1 = new Thread(()->{
            for (int i = 1; i <= 50000; i++) {
                func.add();
            }
        });
        Thread thread2 = new Thread(()->{
            for (int i = 1; i <= 50000; i++) {
                func.add();
            }
        });

        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();

        System.out.println("count = " + func.count);
    }
}